package com.lippeng.taohua.db.util;

import com.lippeng.taohua.db.domain.SuperDomain;
import com.lippeng.taohua.db.page.Page;

/**
 * Build SQL for ImplementDomain
 * 
 * @author lippeng
 * 
 */
public class SqlBuilder {

	private SqlHelper sqlHelper;

	/**
	 * Constructor
	 * 
	 * @param domain
	 */
	public SqlBuilder(SuperDomain domain) {
		this.sqlHelper = new SqlHelper(domain);
	}

	/**
	 * Methods
	 */

	/**
	 * decompose domain and return the insert SQL
	 * 
	 * @param domain
	 * @return
	 */
	public String insert() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("insert into ");// insert start

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append("(");// insert values name start
		sBuilder.append(sqlHelper.getColumnsName());// insert values names
		sBuilder.append(")");// insert values name end

		sBuilder.append(" values ");// values
		sBuilder.append("(");// insert values start
		sBuilder.append(sqlHelper.getValuesName());// insert values
		sBuilder.append(")");// insert values end

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return the delete SQL
	 * 
	 * delete domain by id
	 * 
	 * @param domain
	 * @return
	 */
	public String delete() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("delete from ");// delete start

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" where ");// where
		sBuilder.append("id=");
		sBuilder.append(sqlHelper.getId());// delete condition

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return update SQL
	 * 
	 * if @param domain has null value and DB is not null, don't update
	 * 
	 * @param domain
	 * @return
	 */
	public String update() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("update ");// update start

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" set ");// set
		sBuilder.append(sqlHelper.getUpdateValue());// set values and names

		sBuilder.append(" where ");// where
		sBuilder.append("id=");// id=
		sBuilder.append(sqlHelper.getId());// id value

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return update SQL
	 * 
	 * if @param domain has null value and DB is not null, update null here
	 * 
	 * @param domain
	 * @return
	 */
	public String updateNull() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("update ");// update start

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" set ");// set
		sBuilder.append(sqlHelper.getUpdateNullValue());// set values and names

		sBuilder.append(" where ");// where
		sBuilder.append("id=");// id=
		sBuilder.append(sqlHelper.getId());// id value

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return load SQL
	 * 
	 * Here, load domain by id
	 * 
	 * @param domain
	 * @return
	 */
	public String load() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("select ");// select start
		sBuilder.append("(");
		sBuilder.append(sqlHelper.getAllColumnNames());// all columns
		sBuilder.append(")");

		sBuilder.append(" from ");// from

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" where ");// where
		sBuilder.append("id=");// id=
		sBuilder.append(sqlHelper.getId());// id value

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return count SQL
	 * 
	 * count by select conditions the domain given
	 * 
	 * @param domain
	 * @return
	 */
	public String count() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("select count(*) from ");// select start

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" where ");// where
		sBuilder.append(sqlHelper.getSelectCondition());// Select Conditions

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return select SQL
	 * 
	 * Here is indistinct conditions
	 * 
	 * @param domain
	 * @return
	 */
	public String select() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();

		sBuilder.append("select ");// select start
		sBuilder.append("(");
		sBuilder.append(sqlHelper.getAllColumnNames());// all columns
		sBuilder.append(")");

		sBuilder.append(" from ");// from

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" where ");// where
		sBuilder.append(sqlHelper.getSelectCondition());// Select Conditions

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return select SQL
	 * 
	 * Here is distinct conditions
	 * 
	 * @param domain
	 * @return
	 */
	public String selectEqual() {
		String value = null;

		StringBuilder sBuilder = new StringBuilder();

		sBuilder.append("select ");// select start
		sBuilder.append("(");
		sBuilder.append(sqlHelper.getAllColumnNames());// all columns
		sBuilder.append(")");

		sBuilder.append(" from ");// from

		sBuilder.append(sqlHelper.getTableName());// table name

		sBuilder.append(" where ");// where
		sBuilder.append(sqlHelper.getSelectEqualCondition());// Select
		// Conditions

		sBuilder.append(";");// end

		value = sBuilder.toString();
		return value;
	}

	/**
	 * Decompose domain and return select SQL
	 * 
	 * Here is indistinct conditions and by the given page
	 * 
	 * @param domain
	 * @return
	 */
	public String selectPageList(Page page) {
		String value = null;
		// TODO
		return value;
	}

}
